package com.wuyou.sso2.component;

import com.wuyou.sso.common.entity.Tenant;
import com.wuyou.sso.common.enums.BillTypeEnum;
import com.wuyou.sso2.userdetails.CustomizedTenant;
import com.wuyou.sso2.userdetails.TenantVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 租户详情查询实现
 *
 * @author zouchenglong
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomizedTenantDetailsServiceImpl implements AuthenticationUserDetailsService {

//    @DubboReference(check=false)
//    private RemoteTenantService remoteTenantService;

    /**
     * 获取租户详情
     * Principal里存放tenantId
     * @param token
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
//        Tenant tenant = remoteTenantService.getById((Long)token.getPrincipal());
        Tenant tenant = new Tenant();
        tenant.setId(1L);
        tenant.setName("TestName");
        tenant.setDuty("TestDuty");
        tenant.setCompanyCode("TestCompanyCode");
        tenant.setPutFlag(false);
        tenant.setOperateType("2,3,4,5,6,7,8,9");
        tenant.setTenantType("1");
        tenant.setWarehouses(null);
        tenant.setTallyFlag(true);
        tenant.setEasyFlow("12,13");
        tenant.setMessageType("KWBA,JPPX");
        tenant.setSuperviseCode(null);
        tenant.setSplitFlag(true);
        tenant.setPayFlag(true);
        tenant.setPaymentId("6666000148896495");
        tenant.setOpenInvoiceFlag(true);
        tenant.setInvoiceFlag(true);
        tenant.setAutomaticLockFlag(false);
        tenant.setTrayAutoFlag(false);
        tenant.setStorageUpperLimit(null);
//        tenant.setBillType(BillTypeEnum.SD);
        tenant.setMemorizePasswordsFlag(true);
        tenant.setSdToken(null);
        tenant.setZxgcAppId(null);
        tenant.setZxgcFlag(null);
        tenant.setStorageUpperFlag(null);
        tenant.setUnTrayDownFlag(false);
        TenantVO vo = new TenantVO();
        BeanUtils.copyProperties(tenant, vo);
        CustomizedTenant details = new CustomizedTenant();
        details.setTenantVO(vo);
        return details;
    }
}
